<?php

namespace App\Models;


use App\Helpers\Api\CacheTrait;
use Watson\Rememberable\Rememberable;

class Hero extends Base
{
    use CacheTrait;

    protected $table = 'heros';

    protected $appends = ['pv'];

    public $rememberCacheTag = Hero::class;

    public static function flushTags() {
        \Cache::tags(Hero::class)->flush();
    }

    public function getPvAttribute() {
        return \Cache::tags(Hero::class . '_pv')->rememberForever($this->id, function (){
            return self::find($this->id)->pv_num;
        });
    }

    public function increment($column, $amount = 1, array $extra = [])
    {
        \Cache::tags(Hero::class . '_pv')->increment($this->id);
        return parent::increment($column, $amount, $extra);
    }


    public static function init() {
        self::chunk(100, function ($heros) {
            foreach ($heros as $hero) {
                \Cache::tags('hero_all')->rememberForever($hero->username . $hero->contact_name, function () use($hero) {
                    return $hero;
                });
            }
        });
    }

    public static function getInfo($key) {
        return \Cache::tags('hero_all')->get($key);
    }

}
